/*
** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $
** Some generic functions over Lua objects
** See Copyright Notice in lua.h
*/


#define lobject_c
#define LUA_CORE

#include "lua.h"

#include "lobject.h"


const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL};


int luaO_log2 (unsigned int x) {
	static const lu_byte log_2[256] = {
		0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
	    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
	    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
	    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
	    8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
	    8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
	    8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
	    8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
	};
	int l = -1;
	while (x >= 256) { l += 8; x >>=8; }
	return l + log_2[x];
}


int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
	if (ttype(t1) != ttype(t2)) return 0;
	else switch (ttype(t1)) {
		case LUA_TNIL:
			return 1;
		case LUA_TNUMBER:
			return luai_numeq(nvalue(t1), nvalue(t2));
		case LUA_TBOOLEAN:
			return bvalue(t1) == bvalue(t2);	/* boolean true must be 1 !! */
		case LUA_TLIGHTUSERDATA:
			return pvalue(t1) == pvalue(t2);
		default:
			lua_assert(iscollectable(t1));
			return gcvalue(t1) == gcvalue(t2);
	}
}